package br.com.gustavobhz.portalcolaborador.entidade;

import java.util.List;
import org.hibernate.criterion.Restrictions;
import org.hibernate.Session;
 
public class GenericDAO {
	
    protected Session hibernateSession;
    private Class<?> entityClass;
 
    public GenericDAO(Session hibernateSession, Class<?> entityClass)
    {
        this.hibernateSession = hibernateSession;
        this.entityClass = entityClass;
     }
 
     public List<?> getList()
     {
         return hibernateSession.createCriteria(entityClass).list();
     }
 
    public void gravar(Object entity) 
    {
        hibernateSession.saveOrUpdate(entity);
    }
    
    public void update(Object entity) 
    {
        hibernateSession.update(entity);
    }
 
    public void deleteById(long id)
    {
        hibernateSession.clear();
        hibernateSession.delete(this.getById(id));
     }
 
 public Object getById(long id)
    {
         return hibernateSession.createCriteria(entityClass).add(Restrictions.eq("id", id)).uniqueResult();
     }
 }